﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ForumsBrowser.BusinessObjects;
using System.Windows.Media.Animation;
using ForumsBrowser.Common;
using Microsoft.Win32;

namespace ForumsBrowser
{
    /// <summary>
    /// Interaction logic for ThreadListItem.xaml
    /// </summary>
    public partial class ThreadListItem : UserControl
    {
        public event EventHandler<ThreadSelectedEventArgs> Browse;
        public event EventHandler<ThreadSelectedEventArgs> ViewLastPost;
        public event EventHandler<ThreadSelectedEventArgs> ViewPost;

        public ThreadListItem()
        {
            InitializeComponent();
        }

        private void RaiseEvent(EventHandler<ThreadSelectedEventArgs> handler)
        {
            ThreadSelectedEventArgs args = new ThreadSelectedEventArgs { Thread = this.DataContext as Thread };

            if (handler != null)
                handler(this, args);
            args.Thread.IsNew = false;

            FindVisualChildren<NewThreadNotification>(this).First().Close();
           
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RaiseEvent(ViewLastPost);
            e.Handled = true;
        }

        private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            RaiseEvent(Browse);
            e.Handled = true;
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RaiseEvent(ViewPost);
            e.Handled = true;
        }

        System.Threading.Timer timer = null;

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //timer = new System.Threading.Timer(UpdateTimer, null, 60000, 60000);
        }

        public void UpdateTimer(object value)
        {
        //    tbCreatedOn.Dispatcher.BeginInvoke(new Action(() => BindingOperations.GetBindingExpressionBase(tbCreatedOn, TextBlock.TextProperty).UpdateTarget()));
          //  tbRepliedOn.Dispatcher.BeginInvoke(new Action(() => BindingOperations.GetBindingExpressionBase(tbRepliedOn, TextBlock.TextProperty).UpdateTarget()));
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
         //   timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
           // timer.Dispose();
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }
    }
}
